package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.CareOrderDao;
import com.aaa.doctor.dao.CareOrderItemDao;
import com.aaa.doctor.service.CareOrderItemServivce;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CareOrderItemServivceImpl implements CareOrderItemServivce {

    @Autowired
    private CareOrderItemDao careOrderItemDao;

    @Autowired
    private CareOrderDao careOrderDao;

    /**
     * 查询检查处方
     * @param patientId
     * @return
     */
    @Override
    public Result<List<CareOrderItem>> getInfoByPatientId(String patientId) {
        List<CareOrderItem> list = careOrderItemDao.getInfoByPatientId();
        return new Result<>(200,"查询检查处方成功",list);
    }

    /**
     * 根据itemId删除当前选中行的数据
     * @param itemId
     * @return
     */
    @Override
    public Result<String> deleteCareOrderItemById(String itemId,BigDecimal amount) {
        QueryWrapper<CareOrderItem> wrapper=new QueryWrapper<>();
        wrapper.eq("item_id",itemId);
        CareOrderItem careOrderItem = careOrderItemDao.selectOne(wrapper);
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("co_id",careOrderItem.getCoId());
        CareOrder careOrder = careOrderDao.selectOne(wrapper1);
        // 减去删除的金额
        careOrder.setAllAmount(careOrder.getAllAmount().subtract(amount));
        careOrderDao.updateById(careOrder);
        if (careOrder.getAllAmount().intValue() == 0){
            careOrderDao.delete(wrapper1);
        }
        careOrderItemDao.delete(wrapper);
        return new Result<>(200,"删除药品及检查详情成功");
    }


}
